<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
</head>
<body>
<div id="app">
    <button @click="sortOld">按年龄排序</button>
    <table border="1">
        <tr>
            <th>序号</th>
            <th>ID</th>
            <th>姓名</th>
            <th v-if="index==1">年龄升序</th>
            <th v-if="index==2">年龄降序</th>
            <th v-if="index==0">年龄无序</th>
        </tr>
        <tr v-for="(item,i) in users" :key="item.id">
            <td>{{i+1}}</td>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.old}}</td>
        </tr>
    </table>
</div>
<script>
    var vm=new Vue({
        el:'#app',
        data:{
            users:[
                {id:1,name:'t1',old:23},
                {id:2,name:'t2',old:20},
                {id:3,name:'t3',old:25}
            ],
            index:0
        },
        methods:{
            sortOld(){
                if (this.index==0){
                    this.index=1
                    this.users.sort((a,b)=>{
                        return a.old-b.old
                    })
                }else if (this.index==1){
                    this.index=2
                    this.users.sort((a,b)=>{
                        return b.old-a.old
                    })
                }else if (this.index==2){
                    this.index=0
                    this.users.sort(() => Math.random() - 0.5)
                }
            }
        }
    })
</script>
</body>
</html>
